from  django.shortcuts import HttpResponse
import json
from service.person_node_service import PersonNodeService
from service.person_node_llm_service import PersonNodeLLMService

#查询节点(用数据库方式实现)
def query_node(request):
    if request.method == 'GET':
        person_node_service = PersonNodeService()
        rs = person_node_service.query_node()
        #[{'occupation': '企业家', 'city': '杭州', 'name': '马云', 'age': 58},{'occupation': 'CEO', 'city': '深圳', 'name': '马化腾', 'age': 51}]
        if len(rs)>0:
          #创建一个空列表
          new_list =[]
          for item in rs:
              new_list.append(item["p"])
          print(f"new_list={new_list}")
          data = {"code": 200, "msg": "查询成功", "data": new_list}
        else:
          data = {"code": 500, "msg": "没查询到数据", "data": None}
        return HttpResponse(json.dumps(data))
#查询节点(用大模型方式实现)
def query_llm_node(request):
    if request.method == 'GET':
        person_node_llm_service = PersonNodeLLMService()
        rs = person_node_llm_service.query_node()
        #[{'occupation': '企业家', 'city': '杭州', 'name': '马云', 'age': 58},{'occupation': 'CEO', 'city': '深圳', 'name': '马化腾', 'age': 51}]
        if len(rs)>0:
          data = {"code": 200, "msg": "查询成功", "data": rs}
        else:
          data = {"code": 500, "msg": "没查询到数据", "data": None}
        return HttpResponse(json.dumps(data))

#创建节点(用数据库方式实现)
def create_node(request):
    if request.method == 'GET':
        person_node_service = PersonNodeService()
        #获取前端发送的请求参数
        name = request.GET.get("name")
        age = request.GET.get("age")
        city = request.GET.get("city")
        occupation = request.GET.get("occupation")
        #创建节点
        try:
            person_node_service.create_node(name=name, age=age, city=city, occupation=occupation)
            data = {"code": 200, "msg": "创建成功", "data": None}
            return HttpResponse(json.dumps(data))
        except Exception as e:
            print(e)
            data = {"code": 500, "msg": "创建失败", "data": None}
            return HttpResponse(json.dumps(data))
#创建节点(用大模型方式实现)
def create_llm_node(request):
    if request.method == 'GET':
        person_node_llm_service = PersonNodeLLMService()
        #获取前端发送的请求参数
        name = request.GET.get("name")
        age = request.GET.get("age")
        city = request.GET.get("city")
        occupation = request.GET.get("occupation")
        #创建节点
        try:
            person_node_llm_service.create_node(name=name, age=age, city=city, occupation=occupation)
            data = {"code": 200, "msg": "创建成功", "data": None}
            return HttpResponse(json.dumps(data))
        except Exception as e:
            print(e)
            data = {"code": 500, "msg": "创建失败", "data": None}
            return HttpResponse(json.dumps(data))
#删除节点(用大模型方式实现)
def delete_node(request):
    if request.method == 'GET':
        person_node_llm_service = PersonNodeLLMService()
        #获取前端发送的请求参数
        name = request.GET.get("name")
        #删除节点
        try:
            person_node_llm_service.delete_node(name=name)
            data = {"code": 200, "msg": "删除成功", "data": None}
            return HttpResponse(json.dumps(data))
        except Exception as e:
            print(e)
            data = {"code": 500, "msg": "删除失败"}
            return HttpResponse(json.dumps(data))
#更新节点(用大模型方式实现)
def update_node(request):
     if request.method == 'GET':
         person_node_llm_service = PersonNodeLLMService()
          #获取前端发送的请求参数
         name = request.GET.get("name")
         age = request.GET.get("age")
         city = request.GET.get("city")
         occupation = request.GET.get("occupation")
         try:
             person_node_llm_service.update_node(name=name, age=age, city=city, occupation=occupation)
             data = {"code": 200, "msg": "更新成功", "data": None}
             return HttpResponse(json.dumps(data))
         except Exception as e:
             print(e)
             data = {"code": 500, "msg": "更新失败"}
             return HttpResponse(json.dumps(data))